//
//  UIView+YView.m
//  CodeForConstraint
//
//  Created by 李明明 on 15/10/19.
//  Copyright © 2015年 李明明. All rights reserved.
//

#import "UIView+YView.h"
#import "LManagerProperty.h"
#import <objc/runtime.h>
static const char * key = "faceView";

@implementation UIView (YView)

- (UIView *)faceView
{
    return objc_getAssociatedObject(self, key);
}
- (void)setFaceView:(UIView *)faceView
{
    objc_setAssociatedObject(self, key, faceView, OBJC_ASSOCIATION_RETAIN);
}
- (void)makeConstraintWith:(UIView *)view withProperty:(LManagerProperty *)constraint
{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0f constant:constraint.top];
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:constraint.leading];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-constraint.bottom];
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-constraint.trailing];
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:constraint.height];
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:constraint.width];
//    NSLayoutConstraint *horizontal;
//    NSLayoutConstraint *vertical;
    

//    NSArray *constraints = @[top, left, bottom, right, width, height, horizontal, vertical];
    NSDictionary *dic = @{@"top":top, @"leading":left, @"bottom":bottom, @"trailing":right, @"width":width, @"height":height};
    NSMutableArray *constraints = [NSMutableArray array];
    for (NSString *key in constraint.propertyArr) {
        [constraints addObject:dic[key]];
        if (constraints.count > 4) {
            [constraints removeObject:[constraints firstObject]];
        }
    }
    [NSLayoutConstraint activateConstraints:constraints];
    constraint.propertyArr = [NSMutableArray array];
    
    
}
@end
